﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Admin.IDAO;
using CNKI.TPI.Web.Admin.Model;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace CNKI.TPI.Web.Admin.BLL
{
    public class HomeConfigService : BaseService, IHomeConfigService
    {
        private IHomeConfigDAO homeConfigDao;
        private IHomeDAO homeDao;
        private ISingleDBPublishDAO singDbDao = null;
        ICategoryDAO categoryDAO = null;

        public HomeConfigService(IHomeConfigDAO homeConfigDao, IHomeDAO homeDao, ISingleDBPublishDAO singDbDao, ICategoryDAO categoryDAO)
        {
            this.homeConfigDao = this.generator.CreateInterfaceProxyWithTarget<IHomeConfigDAO>(homeConfigDao, new IInterceptor[] { new LogInterceptor() });
            this.homeDao = this.generator.CreateInterfaceProxyWithTarget<IHomeDAO>(homeDao, new IInterceptor[] { new LogInterceptor() });
            this.singDbDao = this.generator.CreateInterfaceProxyWithTarget<ISingleDBPublishDAO>(singDbDao, new IInterceptor[] { new LogInterceptor() });
            this.categoryDAO = this.generator.CreateInterfaceProxyWithTarget<ICategoryDAO>(categoryDAO, new IInterceptor[] { new LogInterceptor() });
        }
        /// <summary>
        /// 获取首页配置信息 
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfig()
        {
            IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> list = homeDao.GetHomepageConfig();
            if (list != null && list.Count > 0)
            {
                foreach (SYS_CMS_NEWS_NEWSCONTENTCONFIG item in list)
                {
                    SYS_CMS_DATABASE obj = categoryDAO.GetDatabaseByID(item.DbID);//获取数据库名称
                    if(obj!=null)
                    {
                        item.DBName = obj.DatabaseName;

                        int templatetype = obj.DatabaseType;
                        SYS_CMS_RESOURCE_TEMPLATE templateobj = homeConfigDao.GetTemplateByID(templatetype);//获取数据库类型
                        if (templateobj != null)
                        {
                            item.DBType = templateobj.RESOURCENAME;
                        }
                    }
                 
                }
            }
            return list;
        }
        /// <summary>
        /// 根据类型获取首页配置信息
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfig(string showtype)
        {
            return homeConfigDao.GetHomepageConfig(showtype);
        }
        /// <summary>
        /// 获取跳转模板信息
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_JUMPTEMPLATE> GetHomepageJumpConfig()
        {
            IList<SYS_CMS_NEWS_JUMPTEMPLATE> list = homeDao.GetHomepageJumpConfig();
            if (list != null && list.Count > 0)
            {
                foreach (SYS_CMS_NEWS_JUMPTEMPLATE item in list)
                {
                    SYS_CMS_DATABASE obj = categoryDAO.GetDatabaseByID(item.DbID);//获取数据库名称
                    if (obj != null)
                    {
                        item.DBName = obj.DatabaseName;

                        int templatetype = obj.DatabaseType;
                        SYS_CMS_RESOURCE_TEMPLATE templateobj = homeConfigDao.GetTemplateByID(templatetype);//获取数据库类型
                        if (templateobj != null)
                        {
                            item.DBType = templateobj.RESOURCENAME;
                        }
                    }

                }
            }
            return list;
        }

        /// <summary>
        /// 保存设置的栏目
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveContentConfig(SYS_CMS_NEWS_NEWSCONTENTCONFIG obj,string flage)
        {
            bool result = false;
            if (flage == "save")
            {
                result = homeConfigDao.SaveContentConfig(obj);
            }
            else
            {
                result = homeConfigDao.UpdateContentConfig(obj);
            }
            return result;
        }

        /// <summary>
        /// 保存设置的栏目
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveContentJump(SYS_CMS_NEWS_JUMPTEMPLATE obj,string flage)
        {
            bool result = false;
            if (flage == "save")
            {
                result = homeConfigDao.SaveContentJump(obj);
            }
            else
            {
                result = homeConfigDao.UpdateContentJump(obj);
            }
            return result;
        }
        /// <summary>
        /// 判断是否设置过该模板
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool isExitsTemplateContent(int dbid,int type)
        {
            return homeConfigDao.IsExistContentByID(dbid, type);
        }
        /// <summary>
        /// 判断是否设置过该模板
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool isExitsTemplateContent(int dbid, int type,int sysid)
        {
            return homeConfigDao.IsExistContentByID(dbid, type, sysid);
        }
        /// <summary>
        /// 判断是否设置过跳转模板
        /// </summary>
        /// <param name="dbid"></param>
        /// <returns></returns>
        public bool IsExistJumpByID(int dbid,int type)
        {
            return homeConfigDao.IsExistJumpByID(dbid,type);
        }
        /// <summary>
        /// 根据ID获取模板信息
        /// </summary>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> GetHomepageConfigByID(int sysid)
        {
            return homeConfigDao.GetHomepageConfigByID(sysid);
        }
        /// <summary>
        /// 获取调转模板信息
        /// </summary>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_JUMPTEMPLATE> GetHomepageJumpByID(int sysid)
        {
            return homeConfigDao.GetHomepageJumpByID(sysid);
        }
        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool DeleteContentNew(SYS_CMS_NEWS_NEWSCONTENTCONFIG obj)
        {
            return homeConfigDao.DeleteContentNew(obj);
        }
        public bool DeleteJump(SYS_CMS_NEWS_JUMPTEMPLATE obj)
        {
            return homeConfigDao.DeleteJump(obj);
        }

        
        /// <summary>
        /// 修改时，判断排除记录本身
        /// </summary>
        /// <param name="dbid"></param>
        /// <param name="typeid"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistJumpByID(int dbid, int typeid, int sysid)
        {
            return homeConfigDao.IsExistJumpByID(dbid,typeid,sysid);
        }
         /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="exp">查询条件</param>
        /// <returns></returns>
        public IList<SYS_CMS_NEWS_MENUCONFIG> GetMenuList(ExpressionCombine<SYS_CMS_NEWS_MENUCONFIG> exp = null, int cacheTime = 0)
        {
            return homeConfigDao.GetMenuList(exp, cacheTime);
        
        }
        /// <summary>
        /// 保存菜单
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SaveMenu(SYS_CMS_NEWS_MENUCONFIG obj)
        {
            if (obj.SYSID == 0)
            {
                return homeConfigDao.AddMenu(obj);
            }
            else
            {
                return homeConfigDao.UpdateMenu(obj);
            }
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool DelMenu(int sysid)
        {
            SYS_CMS_NEWS_MENUCONFIG obj = new SYS_CMS_NEWS_MENUCONFIG();
            obj.SYSID = sysid;
            return homeConfigDao.DelMenu(obj);
        }
        /// <summary>
        /// 更新菜单显示顺序
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool UpdateMenuSort(IList<SYS_CMS_NEWS_MENUCONFIG> list)
        {
            return homeConfigDao.UpdateMenuSort(list);
            
        }
         /// <summary>
        /// 更改首页栏目显示顺序
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool UpdateHomeSort(IList<SYS_CMS_NEWS_NEWSCONTENTCONFIG> list)
        {
            return homeConfigDao.UpdateHomeSort(list);
        }

    }
}
